package J.算法.回溯算法;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class _46全排列 {

    List<List<Integer>> res = new LinkedList<>();
    public List<List<Integer>> permute(int[] nums) {
        List<Integer> track = new LinkedList<>();
        backtrack(nums,track);
        return res;

    }

    /**
     * nums为选择 track为路径
     * @param nums
     * @param track
     */
    public void backtrack(int[] nums,List<Integer> track){
        if (track.size() == nums.length){
            //此处 如果直接传递的是track 那么结果就是全空 值传递
            res.add(new ArrayList<>(track));
            return;
        }

        for (int i = 0; i < nums.length; i++) {
            if (track.contains(nums[i])){
                continue;
            }

            track.add(nums[i]);

            backtrack(nums,track);
            track.remove(track.size()-1);

        }
    }

}
